package servlets;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.StringUtils;

import beans.ComparativaBean;
import beans.ItemComparativaBean;
import controller.BusinessDelegate;

public class FiltrarListado extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BusinessDelegate bDel = new BusinessDelegate();

	private List<ComparativaBean> lista;

	public FiltrarListado() throws RemoteException {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			lista = bDel.getListaComparativa();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		HttpSession session = request.getSession(true);
		List<ItemComparativaBean> lcb = new ArrayList<ItemComparativaBean>();
		if("Ver Mejores Precios".equalsIgnoreCase(request.getParameter("action"))){
			for(ComparativaBean cb : lista){
				lcb.add(cb.getMejorPrecio());
			}
		}else{
			
		
		
		for(ComparativaBean cb : lista){
			lcb.addAll(cb.getItemsLista());
		}
		
		}
		String filtro;
		filtro = request.getParameter("filtroMarca");
		if(!StringUtils.isEmpty(filtro)){
			for(ComparativaBean il : lista ){
					if(!il.getMarca().equalsIgnoreCase(filtro))
						lcb.removeAll(il.getItemsLista());
				}
			request.setAttribute("filtroMarca", filtro);
		}
		/********************************************************/
		filtro = request.getParameter("filtroOrigen");
		if(!StringUtils.isEmpty(filtro)){
			for(ComparativaBean il : lista ){
				if(!il.getItemsLista().iterator().next().getRodamiento().getOrigen().equalsIgnoreCase(filtro))
					lcb.removeAll(il.getItemsLista());
			}
			request.setAttribute("filtroOrigen", filtro);
		}
		/********************************************************/
		filtro = request.getParameter("filtroNro");
		if(!StringUtils.isEmpty(filtro)){
			for(ComparativaBean il : lista ){
				if(!il.getNroRodamiento().equalsIgnoreCase(filtro))
					lcb.removeAll(il.getItemsLista());
			}
			request.setAttribute("filtroNro", filtro);
		}
		/********************************************************/
		session.setAttribute("listaFiltrada", lcb);
		 RequestDispatcher dispatcher = request.getRequestDispatcher("/verListaFiltrada.jsp");
	    try {
	        dispatcher.forward(request, response);
		} catch (ServletException e) {
		        e.printStackTrace();
		} catch (IOException e) {
		        e.printStackTrace();
		}	
	}
}
